//1.me
class Solution {
public:
    string addBinary(string a, string b) {
        if(a=="0")
            return b;
        if(b=="0")
            return a;

        string ret;
        int cur = 0;
        int jinwei = 0;
        int i=a.size()-1,j=b.size()-1;
        while(i>=0 || j>=0) {
            if(i>=0)
                cur = cur + (a[i--]-'0');
             if(j>=0)
                cur = cur + (b[j--]-'0');
          
            int flag = cur+jinwei;
            cur= (cur+jinwei)%2;
            if(flag > 1) 
                jinwei = 1;
            else 
                jinwei=0;       
                          
            ret.push_back(cur+'0');
            cur=0;

        }
        if(jinwei==1)
            ret.push_back(jinwei+'0');
        reverse(ret.begin(), ret.end());
        return ret;

    }
};

//2.简洁
class Solution {
public:
    string addBinary(string a, string b) {
        int i = a.size() - 1, 
            j = b.size() - 1, 
            carry = 0;//进位
        string res;

        while (i >= 0 || j >= 0 || carry) {
            int sum = carry;
            if (i >= 0) sum += a[i--] - '0';
            if (j >= 0) sum += b[j--] - '0';

            // 当前位 = sum & 1；进位 = sum >> 1
            res.push_back(char('0' + (sum & 1)));
            carry = sum >> 1;
        }

        reverse(res.begin(), res.end());
        return res;
    }
};





